﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Nhibernate;
using NHibernate.Criterion;
using NHibernate;

namespace Bussiness.Service
{
    public class User_Service
    {
        public List<User> getAllVATNSD()
        {
            return new HibernateDAO<User, String>().getByCriterion();
        }
        public void deleteVATNSD(int id)
        {
            new HibernateDAO<User, string>().ExecuteSql("delete User where id = '" + id + "'");
        }

        public void saveVATNSD(User vatKHACHHANG)
        {
            new HibernateDAO<User, String>().SaveOrUpdate(vatKHACHHANG);
        }

        public User searchVATNSD_ByID(string ma)
        {
            HibernateDAO<User, String> dao = new HibernateDAO<User, String>();
            if (!Formater.isNull(ma))
                dao.addSearchCriteria(Expression.Eq("Id", int.Parse(ma)));
            List<User> temp = dao.getByCriterion();

            foreach (User sv in temp)
            {
                return sv;
            }
            return null;
        }

        public User searchVATNSD_ByTenNSD(string tenNSD)
        {
            HibernateDAO<User, String> dao = new HibernateDAO<User, String>();
            if (!Formater.isNull(tenNSD))
                dao.addSearchCriteria(Expression.Eq("User1", tenNSD));
            List<User> temp = dao.getByCriterion();

            foreach (User sv in temp)
            {
                return sv;
            }
            return null;
        }



        public void changePassword(string id, string newPassword)
        {
            ISession session = null;
            ITransaction transaction = null;
            try
            {
                session = NHibernateSessionManager.GetCurrentSession();
                transaction = session.BeginTransaction();
                //------------------- Update SendDirectHeader -----------------------------------
                String hql = "UPDATE User set pass = :mat_khau WHERE id = :id";

                IQuery query = session.CreateSQLQuery(hql).AddEntity(typeof(User));
                query.SetString("mat_khau", newPassword);
                query.SetInt32("id", int.Parse(id));

                int result = query.ExecuteUpdate();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                throw ex;
            }
            finally
            {
                NHibernateSessionManager.CloseSession();
            }
        }
    
        
    }
}
